/**
 * 
 */
package com.yinhai.activity;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yinhai.adapter.ContactGroupContactListAdpter;
import com.yinhai.constant.Contact;
import com.yinhai.model.ContactsUser;
import com.yinhai.service.ContactUserServiceImpl;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;


/**
 * @author Juyon
 * 
 */
public class ContactListFragment extends Fragment {
	private ListView contactListView;
	private ContactGroupContactListAdpter smsAdpter;
	private ContactUserServiceImpl contactUserService;
	private List<ContactsUser> list;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.main_list_contact, null);
		Bundle bundle = this.getArguments();
		String contactGroupId = bundle.getString("contactGroupId"); 
		contactUserService = new ContactUserServiceImpl(this.getActivity());
		if(Contact.LOCAL_CONTACT_GROUP_ID.endsWith(contactGroupId)) {
			list = contactUserService.findLocalContact();
		} else {
			list = contactUserService.findContactsUserByGroupId(contactGroupId);
		}
		
		contactListView = (ListView)view.findViewById(R.id.contact_list_item);
		smsAdpter = new ContactGroupContactListAdpter(this.getActivity());
		smsAdpter.assignment(list);
		contactListView.setAdapter(smsAdpter);
		contactListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Map<String, String> map = new HashMap<String, String>();
				ContactsUser contactUser = (ContactsUser) smsAdpter.getItem(position);
				FragmentTransaction transaction = getActivity().getSupportFragmentManager()
						.beginTransaction();
				Fragment fragment = null;
				Bundle bundle = new Bundle();
				//当点击的数据为集团通讯录时查看集团通讯录中通讯人信息
				if(Contact.CONTACT_TYPE_ONLINE.equals(contactUser.getType())) {
					fragment = new OnlineContactDetailFragment();
				}
				bundle.putString("userId", contactUser.getUserid());
			    if(fragment != null) {
			    	fragment.setArguments(bundle);
					transaction.replace(R.id.container, fragment);
					transaction.commit();
			    }
				
			}
		});
		return view;
	}

	
}
